package 排序2;

public class Sort {

    /**
     * 插入排序
     * 时间复杂度:O(N^2)最坏时间复杂度
     * 最好时间复杂度:O(N)
     * 空间复杂度O(1)
     * 稳定性:稳定
     */
    public static void InsertSort(int[] array) {

        for (int i = 1; i < array.length; i++) {
            int j = i-1;
            int tmp = array[i];
            for (; j >=0 ; j--) {
                if(array[j] > tmp) {
                    array[j+1] = array[j];
                }else {
                    break;
                }
            }
            array[j+1] = tmp;
        }
    }
    public static void ShellSort(int[] array) {
        int gap = array.length-1;
        while (gap != 0) {
            gap /= 2;
        InsertSort(array,gap);
        }
        InsertSort(array);
    }
    public static void InsertSort(int[] array,int gap) {
        for (int i = gap; i < array.length; i++) {
            int j = i-gap;
            int tmp = array[i];
            for (; j >=0 ; j-=gap) {
                if(array[j] > tmp) {
                    array[j+gap] = array[j];
                }else {
                    break;
                }
            }
            array[j+gap] = tmp;
        }
    }




}
